package com.yuju.controller.accounts;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.account.dao.entity.UnitAccount;
import com.yuju.account.enums.AccountStatusEnum;
import com.yuju.account.enums.AccountTypeEnum;
import com.yuju.account.enums.BusinessTypeDetailEnum;
import com.yuju.account.enums.BusinessTypeEnum;
import com.yuju.account.enums.UnitAccountReturnCodeEnum;
import com.yuju.account.model.UnitAccountModel;
import com.yuju.account.service.IUnitAccountService;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.util.Utils;
import com.yuju.vo.account.UnitAccountVO;

/**
 * 类名：UnitAccountController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月27日 下午1:55:18  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("unitAccount")
public class UnitAccountController extends BaseController<UnitAccountVO, UnitAccountModel> {

	@Autowired
	private IUnitAccountService unitAccountService;
	
	@RequestMapping("listUnitAccountSum")
	@ResponseBody
	public Result listUnitAccountSum(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		BigDecimal sum = unitAccountService.listUnitAccountSum(convert(unitAccountVO));
		Result result = new Result();
		if(Utils.isNotNull(sum)){
			result.setData(sum);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SUM_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SUM_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SUM_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SUM_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("getUnitAccountByBookId")
	@ResponseBody
	public Result getUnitAccountByBookId(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		List<UnitAccountModel> list = unitAccountService.list(convert(unitAccountVO));
		BigDecimal ysSum = new BigDecimal("0");
		BigDecimal ssSum = new BigDecimal("0");
		if(Utils.isNotNull(list) && list.size()>0){
			for (UnitAccountModel model : list) {
				if(model.getAccountType() == AccountTypeEnum.ACCOUNT_TYPE_YS.getValue()){
					ysSum = ysSum.add(model.getMoney());
				}else{
					ssSum = ssSum.add(model.getMoney());
				}
			}
		}
		Result result = new Result();
		Map<String, Object> map = new HashMap<>();
		map.put("ssSum", ssSum);
		map.put("ysSum", ysSum);
		if(Utils.isNotNull(list) && list.size()>0){
			result.setRows(list);
			result.setData(map);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_FAILURE.getDesc());
		}
		return result;
	}
	
	@RequestMapping("getUnitAccountSubreport")
	@ResponseBody
	public Result getUnitAccountSubreport(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		PageModel pageModel = null;
		List<UnitAccountModel> list = null;
		if(Utils.isNotNull(unitAccountVO.getPage())){
			pageModel = unitAccountService.listUnitAccountSS(convert(unitAccountVO), unitAccountVO.getPage(), unitAccountVO.getPageSize());
		}else{
			list = unitAccountService.listUnitAccountSSNoPage(convert(unitAccountVO));
		}
		Result result = new Result();
		if(Utils.isNotNull(pageModel)){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SUBREPORT_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SUBREPORT_SUCCESS.getDesc());
		}else if(Utils.isNotNull(list)){
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SUBREPORT_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SUBREPORT_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SUBREPORT_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SUBREPORT_FAILURE.getDesc());
		}
		return result;
	}
	@RequestMapping("getYSK")
	@ResponseBody
	public Result getYSK(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		Map<Object, Object> map = unitAccountService.getYSK(convert(unitAccountVO));
		Result result = new Result();
		if(Utils.isNotNull(map)){
			result.setData(map);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：单位账务处理列表<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月16日 上午11:13:11 <br/>
	 */
	@RequestMapping("listUnitAccountAndSum")
	@ResponseBody
	public Result listUnitAccountAndSum(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		Map<String, String> fail = FieldValidator.validateAll(unitAccountVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		PageModel pageModel = unitAccountService.listUnitAccountAndSum(convert(unitAccountVO), unitAccountVO.getPage(), unitAccountVO.getPageSize());
		Result result = new Result();
		if(Utils.isNotNull(pageModel)){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：单位 实收账务查询<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月19日 下午4:55:37 <br/>
	 */
	@RequestMapping("listUnitAccountSS")
	@ResponseBody
	public Result listUnitAccountSS(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		Map<String, String> fail = FieldValidator.validateAll(unitAccountVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		PageModel pageModel = unitAccountService.listUnitAccountSS(convert(unitAccountVO), unitAccountVO.getPage(), unitAccountVO.getPageSize());
		Result result = new Result();
		if(Utils.isNotNull(pageModel) && pageModel.getTotal()>0){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_SUCCESS.getDesc());
		}else if(Utils.isNotNull(pageModel) && (pageModel.getTotal()<=0 || Utils.isNotNull(pageModel.getTotal()))){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_QUERY_NOTHING.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_QUERY_NOTHING.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_DETAILLIST_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：单位账务列表查询<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月15日 下午2:50:57 <br/>
	 */
	@RequestMapping("listUnitAccount")
	@ResponseBody
	public Result listUnitAccount(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		PageModel pageModel = null;
		List<UnitAccount> list = null;
		if(Utils.isNotNull(unitAccountVO.getPage())){
			pageModel = unitAccountService.pageListUnitAccount(convert(unitAccountVO), unitAccountVO.getPage(), unitAccountVO.getPageSize());
		}else{
			list = unitAccountService.listUnitAccount(convert(unitAccountVO));
		}
		Result result = new Result();
		if(Utils.isNotNull(pageModel)){
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_SUCCESS.getDesc());
		}else if(Utils.isNotNull(list)){
			result.setRows(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_SUCCESS.getDesc());
		}
		else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_LIST_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：增加账务（不结算）<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月14日 下午4:50:22 <br/>
	 */
	@RequestMapping("saveUnitAccount")
	@ResponseBody
	public Result saveUnitAccount(@RequestBody JSONObject obj){
		Map<String, Object> param = new HashMap<>();
		param.put("accountBookId", obj.getString("accountBookId"));
		param.put("empId", obj.getString("empId"));
		param.put("hotelId", obj.getString("hotelId"));
		param.put("groupId", obj.getString("groupId"));
		param.put("hotelCode", obj.getString("hotelCode"));
		param.put("customId", obj.getString("customId"));
		param.put("customType", obj.getByte("customType"));
		
		JSONArray jsonArray = obj.getJSONArray("unitAccountModelList");
		String jsonString = jsonArray.toJSONString();
		ArrayList<UnitAccountModel> unitAccountModelList = JSON.parseObject(jsonString,new TypeReference<ArrayList<UnitAccountModel>>(){});
		param.put("unitAccountModelList", unitAccountModelList);
		
		String unitAccountBookId  = unitAccountService.saveUnitAccountList(param);
		Result result = new Result();
		if(Utils.isNotNull(unitAccountBookId)){
			result.setData(unitAccountBookId);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：单位账务结算<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月8日 下午2:31:36 <br/>
	 */
	@RequestMapping("settle")
	@ResponseBody
	public Result settle(@RequestBody JSONObject obj){
		//获得帐套ID
		String accountBookId = obj.getString("accountBookId");
		String empId = obj.getString("empId");
		String hotelId = obj.getString("hotelId");
		String groupId = obj.getString("groupId");
		String hotelCode = obj.getString("hotelCode");
		
		String customId = obj.getString("customId");
		Byte customType = obj.getByte("customType");
		
		//获得新增账务集合
		JSONArray jsonArray = obj.getJSONArray("unitAccountModelList");
		String jsonString = jsonArray.toJSONString();
		ArrayList<UnitAccountModel> unitAccountModelList = JSON.parseObject(jsonString,new TypeReference<ArrayList<UnitAccountModel>>(){});
		//获得结算账务的id集合
		JSONArray json = obj.getJSONArray("unitAccountIdList");
		String jString = json.toJSONString();
		ArrayList<String> unitAccountIdList = JSON.parseObject(jString,new TypeReference<ArrayList<String>>(){});
		
		Map<String, Object> param = new HashMap<>();
		param.put("accountBookId", accountBookId);
		param.put("unitAccountModelList", unitAccountModelList);
		param.put("unitAccountIdList", unitAccountIdList);
		param.put("empId", empId);
		param.put("hotelId", hotelId);
		param.put("groupId", groupId);
		param.put("hotelCode", hotelCode);
		param.put("customId", customId);
		param.put("customType", customType);
		
		String unitAccountBookId = unitAccountService.saveSettle(param);
		Result result = new Result();
		if(Utils.isNotNull(unitAccountBookId)){
			result.setData(unitAccountBookId);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SETTLE_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SETTLE_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_SETTLE_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_SETTLE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：新增预收款<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月7日 上午11:46:30 <br/>
	 */
	@RequestMapping("saveYSK")
	@ResponseBody
	public Result saveYSK(@RequestBody JSONObject obj){
		UnitAccountVO unitAccountVO = obj.toJavaObject(UnitAccountVO.class);
		unitAccountVO.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_SS.getValue());
		unitAccountVO.setBusinessType(BusinessTypeEnum.BUSINESS_TYPE_YSK.getValue());
		unitAccountVO.setBusinessTypeName(BusinessTypeEnum.getName(unitAccountVO.getBusinessType()));
		unitAccountVO.setBusinessTypeDetailName(BusinessTypeDetailEnum.getName(unitAccountVO.getBusinessTypeDetail()));
		unitAccountVO.setBusinessDetail(BusinessTypeDetailEnum.getName(unitAccountVO.getBusinessTypeDetail())+BusinessTypeEnum.getName(unitAccountVO.getBusinessType()));
		unitAccountVO.setAccountStatus(AccountStatusEnum.STATUS_OK.getValue());
		unitAccountVO.setAccountStatusName(AccountStatusEnum.STATUS_OK.getName());
		unitAccountVO.setCreateUserId(unitAccountVO.getEmpId());
		int i = unitAccountService.saveUnitAccount(convert(unitAccountVO));
		
		Result result = new Result();
		if(i>0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_YSK_SUCCESS.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_YSK_SUCCESS.getDesc());
		}else{
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(UnitAccountReturnCodeEnum.UNITACCOUNT_YSK_FAILURE.getCode());
			result.setReturnMsg(UnitAccountReturnCodeEnum.UNITACCOUNT_YSK_FAILURE.getDesc());
		}
		return result;
	}
	
}
